package com.unhz.taigu.controller;



import com.unhz.generator.enums.EnumTryOrder;
import com.unhz.generator.pojo.TryOrder;
import com.unhz.generator.utils.EmptyUtil;
import com.unhz.generator.utils.JsonResult;
import com.unhz.generator.utils.Page;
import com.unhz.generator.utils.PageSize;
import com.unhz.taigu.service.TryOrderInfoService;
import com.unhz.taigu.service.TryOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * @Author: 魏运奇
 * @Description:
 * @Date: Created in 下午 5:32 2018/1/31 0031
 * @Modified By: V1.0.0
 */
@Controller
@RequestMapping("/tryOrder")
public class TryOrderController {

     @Autowired
     private TryOrderService tryOrderService;
     @Autowired
     private TryOrderInfoService tryOrderInfoService;

     /**
      * @Param pageNo 页数
      * @Param keywords  关键词
      * */
     @RequestMapping("/selectList")
     public ModelAndView selectInsole(@RequestParam(value = "pageNo",defaultValue = "1",required = false)String pageNo,
                                      @RequestParam(value = "keywords",defaultValue = "",required = false)String keywords,
                                      @RequestParam(value = "adminId",defaultValue = "",required = false)Integer adminId,
                                      @RequestParam(value = "roleId",defaultValue = "",required = false)Integer roleId) {
          System.out.print("成功进入控制器");
          ModelAndView mav = new ModelAndView("try/list.jsp");
          Page<TryOrder> page = tryOrderService.selectTryOrderList(pageNo, PageSize.pageSize, keywords,adminId,roleId);
          //查询试做信息列表
          mav.addObject("page", page);
          List<TryOrder> list = page.getRecords();
          return mav;
     }

     /**
      * 该变试做订单状态
      * @param id 试做id
      * @param status 状态
      * @return
      */
     @RequestMapping("/changeStatus")
     @ResponseBody
     public JsonResult changeStatus(@RequestParam(value="id")String id,
                                    @RequestParam(value="status",required = false)String status){
          String newStaus="";
          Date finishTime=null;
          //状态为1时表示申请中，改变为试做中
          if(!EmptyUtil.isEmpty(status)){
               if(String.valueOf(EnumTryOrder.APPLICATION.getIndex()).equals(status)){
                    newStaus=String.valueOf(EnumTryOrder.TRY.getIndex());
                    tryOrderService.changsStatus(Long.parseLong(id),Integer.parseInt(newStaus),finishTime);
                    return JsonResult.success("试做成功！");
               }
               //状态为2时表示试做中，改变为完成  此时生成完成时间
               if(String.valueOf(EnumTryOrder.TRY.getIndex()).equals(status)){
                    newStaus=String.valueOf(EnumTryOrder.TRYCOMPLETE.getIndex());
                    finishTime=new Date();
                    tryOrderService.changsStatus(Long.parseLong(id),Integer.parseInt(newStaus),finishTime);
                    return JsonResult.success("试做完成！");
               }
          }
          return JsonResult.failure("操作失败！");
     }

     /**
      * 删除试做订单
      * @param id 订单id
      * @return
      */
     @RequestMapping("/delete")
     @ResponseBody
     public JsonResult delete(@RequestParam(value = "id",required = false)String id){
          boolean flag=tryOrderService.deleteById(Long.parseLong(id));
          if(flag){
               return  JsonResult.success("删除成功！");
          }
          return JsonResult.failure("删除失败！");
     }



}










